<template>

  <div class="add-edit-tenant-view" v-loading="loading">
    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="140px" class="demo-ruleForm">
      <el-form-item :label="$t('tables_title.tenant_name')" prop="name">
        <el-input v-model="ruleForm.name" :placeholder="$t('tables_title.tenant_name')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.tenant_code')" prop="code">
        <el-input v-model="ruleForm.code" :placeholder="$t('tables_title.tenant_code')"></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.account')" prop="adminUsername" v-if="!this.isEdit">
        <el-input v-model="ruleForm.adminUsername" disabled></el-input>
      </el-form-item>
      <el-form-item :label="$t('tables_title.password')" prop="adminPassword" v-if="!this.isEdit">
        <el-input type="password" v-model="ruleForm.adminPassword" :placeholder="$t('tables_title.password')"></el-input>
      </el-form-item>
      <div style="text-align: center;">
        <el-button type="primary" @click="submitForm('ruleForm')">{{$t('global.submit')}}</el-button>
        <el-button @click="cancelView">{{$t('global.cancel')}}</el-button>
      </div>
    </el-form>
  </div>

</template>

<script>

import { addTenant,editTenant } from '@/api/tenantApi.js'

export default {
  data(){
    return{
      loading:false,
      userInfo:{},
      ruleForm:{
        name:'',
        code:'',
        adminUsername:'',
        adminPassword:'',
      },
      rules:{
          name: [
            { required: true, message: this.$t('tenant_management.empty_name'), trigger: 'blur' },
          ],
          code: [
            { required: true, message: this.$t('tenant_management.empty_code'), trigger: 'blur' },
          ],
          adminUsername: [
            { required: !this.isEdit, message: this.$t('user_management.empty_account'), trigger: 'blur' },
          ],
          adminPassword: [
            { required: !this.isEdit, message: this.$t('user_management.empty_password'), trigger: 'blur' },
          ],
      }
    }
  },
  props: ['isEdit','editTenantInfo'],
  watch:{
    editTenantInfo(newVal,oldVal){
      this.ruleForm = newVal
    }
  },
  components:{

  },
  created(){
     this.ruleForm = this.editTenantInfo
     console.log(this.userInfo)
  },
  methods:{
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let formData = new FormData()
          formData.append("name",this.ruleForm.name)
          formData.append("code",this.ruleForm.code)

          if(this.ruleForm.tenantId){
            formData.append("tenantId",this.ruleForm.tenantId)
            this.userEditTenant(formData)
          }else{
            formData.append("adminUsername",this.ruleForm.adminUsername)
            formData.append("adminPassword",this.ruleForm.adminPassword)
            this.userAddTenant(formData)
          }
        } else {
          return false;
        }
      });
    },
    userEditTenant(fromdata){         //修改Tenant信息
      this.loading = true
      editTenant(fromdata).then((res)=>{
        this.loading = false
        console.log(res)
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.modified_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    userAddTenant(fromdata){          //新增Tenant
      this.loading = true
      addTenant(fromdata).then((res)=>{
        this.loading = false
        if(res.data.code == 200){
          this.$message({
            message: this.$t('global.added_success'),
            type: 'success'
          });
          this.refreshData()
        }else{
          this.$message.error(res.data.message)
        }
      }).catch((e)=>{

      })
    },
    cancelView(){       //关闭弹窗
      this.$emit('cancelView')
    },
    refreshData(){
      this.$emit('refreshData')
    }
  }
}
</script>

<style lang="scss" scoped>
.add-edit-tenant-view{
  padding: 0 30px;
}
</style>
